//
//  ViewController.m
//  GameKit
//
//  Created by 刘家俊 on 17/3/31.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "ViewController.h"
#import <GameKit/GameKit.h>

@interface ViewController () <GKPeerPickerControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *ImageView;
/* 保存当前会话 */
@property (nonatomic, strong) GKSession *session;


@end


@implementation ViewController

#warning - 这个工程成功连接两个设备的话，需要有第三方开热点或者连接wifi,GameKit在ios7之后就不用了

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.ImageView.clipsToBounds = YES;
    
}


#pragma mark - <GKPeerPickerControllerDelegate>

/**
 连接蓝牙的方法
 @param type 附近、在线
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type
{
    NSLog(@"%s  %d  type = %lu  pick = %@",__func__,__LINE__,(unsigned long)type,picker);
}


/**
 连接会话的方式
 @param type 附近、在线
 */
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
    return nil;
}

/**
 连接成功
 @param peerID 连接成功的设备id
 @param session 当前会话 （只需要保存当前的会话，即可数据传递）
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
    //连接成功隐藏选择器
    [picker dismiss];
    
    //接收数据的回调 GameKit 必须实现
    [session setDataReceiveHandler:self withContext:nil];
    
    //保存会话
    self.session = session;
}


/**
 只要有数据回来，那么就会调用
 */
- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
    if (!data) {
        return;
    }
    //转换图片
    UIImage *img = [UIImage imageWithData:data];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.ImageView.image = img;
    });
}


/**
 退出
 */
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
    
}

- (IBAction)selectedEquipment:(id)sender {
    
    //创建蓝牙选择器
    GKPeerPickerController *picker = [[GKPeerPickerController alloc] init];
    picker.delegate = self;
    [picker show];
}



- (IBAction)selectedImage:(id)sender {
    UIImagePickerController *imagepic = [[UIImagePickerController alloc] init];
    imagepic.delegate = self;
    imagepic.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imagepic animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    self.ImageView.image = info[UIImagePickerControllerOriginalImage];
    [picker dismissModalViewControllerAnimated:YES];
}

- (IBAction)sendImage:(id)sender {
    
    //转换image
    if (!self.ImageView.image) {
        return;
    }
    NSData *data = UIImagePNGRepresentation(self.ImageView.image);
    
    //GKSendDataUnreliable 不安安模式
    //GKSendDataReliable  安全模式
    [self.session sendDataToAllPeers:data withDataMode:GKSendDataUnreliable error:nil];
    
    NSLog(@"self.session = %@",self.session);
    
}

@end
